<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
<!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-message" class="ml-5" v-model="email"></el-input>-->
<!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-position" class="ml-5" v-model="address"></el-input>-->
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

<!--    <div style="margin: 10px 0">-->
<!--      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>-->
<!--      <el-popconfirm-->
<!--          class="ml-5"-->
<!--          confirm-button-text='确定'-->
<!--          cancel-button-text='我再想想'-->
<!--          icon="el-icon-info"-->
<!--          icon-color="red"-->
<!--          title="您确定批量删除这些数据吗？"-->
<!--          @confirm="delBatch">-->
<!--        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>-->
<!--      </el-popconfirm>-->
<!--      &lt;!&ndash; <el-upload action="http://localhost:9090/judge/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">-->
<!--        <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>-->
<!--      </el-upload>-->
<!--      <el-button type="primary" @click="exp" class="ml-5">导出 <i class="el-icon-top"></i></el-button> &ndash;&gt;-->
<!--    </div>-->

    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
      <el-table-column prop="id" label="ID" width="80" v-if="false"></el-table-column>
      <el-table-column prop="name" label="姓名" align="center" width="80"></el-table-column>
      <el-table-column prop="sex" width="50" align="center" :formatter="sexFormatter" label="性别"></el-table-column>
      <el-table-column prop="idcard" width="180" align="center" label="身份证号"></el-table-column>
      <el-table-column prop="phone" align="center" width="100" label="手机"></el-table-column>
      <el-table-column label="图片" align="center"  width="150px"><template slot-scope="scope"><el-image style="width: 100px; height: 100px" :src="scope.row.avatar" :preview-src-list="[scope.row.img]"></el-image></template></el-table-column>
      <el-table-column prop="representUnit" align="center" label="代表单位"></el-table-column>
      <el-table-column prop="workUnit"  align="center" label="工作单位"></el-table-column>
      <el-table-column prop="judgeLevel" align="center" :formatter="levelFormatter"  label="现裁判等级"></el-table-column>
      <el-table-column prop="passDate"  align="center" label="现等级通过时间"></el-table-column>
      <el-table-column prop="latestRecord" :show-overflow-tooltip="true" align="center" label="最近执裁或复训活动名称及地点"></el-table-column>
      <el-table-column prop="raceName" align="center"  :show-overflow-tooltip="true" label="报名赛事"></el-table-column>
      <el-table-column prop="raceId" align="center"  label="赛事id" v-if="false"></el-table-column>
      <el-table-column prop="isAccept" align="center" label="是否接受" :formatter="acceptFormatter"></el-table-column>
      <el-table-column prop="post"  align="center" label="选派岗位" :formatter="postFormatter"></el-table-column>

      <el-table-column label="操作"  width="280px" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">选派岗位 </el-button><!--<i class="el-icon-edit"></i>-->
<!--          <el-popconfirm-->
<!--              class="ml-5"-->
<!--              confirm-button-text='确定'-->
<!--              cancel-button-text='我再想想'-->
<!--              icon="el-icon-info"-->
<!--              icon-color="red"-->
<!--              title="您确定拒绝吗？"-->
<!--              @confirm="refuse(scope.row)"-->
<!--          >-->
            <el-button type="text" v-if="scope.row.isAccept ==='0'"  @click="refuse(scope.row)" slot="reference">拒绝 </el-button><!--<i class="el-icon-remove-outline"></i>-->
<!--          </el-popconfirm>-->
            <el-button type="text" v-else slot="reference" @click="refuse(scope.row)">接受 </el-button><!--<i class="el-icon-remove-outline"></i>-->

          <el-button type="text" @click="queryRecord(scope.row)">查看执裁记录 </el-button><!--<i class="el-icon-edit"></i>-->
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="信息" :visible.sync="dialogFormVisible" width="40%" :close-on-click-modal="false">
      <el-form label-width="120px" size="small" style="width: 80%; margin: 0 auto">
        <el-form-item prop="name" label="姓名">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="sex" label="性别">
          <el-input v-model="form.sex" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="idcard" label="身份证号">
          <el-input v-model="form.idcard" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="phone" label="手机">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="avatar" label="证件照">
          <el-upload action="http://localhost:9090/file/upload" ref="img" :on-success="handleImgUploadSuccess">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item prop="representUnit" label="代表单位">
          <el-input v-model="form.representUnit" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="workUnit" label="工作单位">
          <el-input v-model="form.workUnit" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="judgeLevel" label="现裁判等级">
          <el-input v-model="form.judgeLevel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="passDate" label="现等级通过时间">
          <el-date-picker v-model="form.passDate" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item prop="latestRecord" label="最近执裁或复训活动名称及地点">
          <el-input v-model="form.latestRecord" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="选派岗位" :visible.sync="dialogPostFormVisible">
      <template>
        <el-radio-group v-model="post">
          <el-radio :label="0">技术代表</el-radio>
          <el-radio :label="1">仲裁</el-radio>
          <el-radio :label="2">裁判长</el-radio>
          <el-radio :label="3">副裁判长</el-radio>
          <el-radio :label="4">裁判员</el-radio>
        </el-radio-group>
      </template>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogPostFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="selectPost">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="执裁记录" :visible.sync="dialogTableVisible">
<!--      <div style="margin: 10px 0">-->
<!--        <el-date-picker type="date" value-format="yyyy-MM-dd" placeholder="请选择赛事开始日期" suffix-icon="el-icon-search" v-model="raceDate"></el-date-picker>-->
<!--&lt;!&ndash;        <el-select v-model="raceName" placeholder="请选择裁判等级">&ndash;&gt;-->
<!--&lt;!&ndash;          <el-option v-for="item in raceNames"&ndash;&gt;-->
<!--&lt;!&ndash;                     :key="item.value"&ndash;&gt;-->
<!--&lt;!&ndash;                     :label="item.label"&ndash;&gt;-->
<!--&lt;!&ndash;                     :value="item.value">&ndash;&gt;-->
<!--&lt;!&ndash;          </el-option>&ndash;&gt;-->
<!--&lt;!&ndash;        </el-select>&ndash;&gt;-->
<!--        <el-button class="ml-5" type="primary" @click="loadRecord">搜索</el-button>-->
<!--        <el-button type="warning" @click="recordReset">重置</el-button>-->
<!--      </div>-->
      <el-table :data="recordData">
        <el-table-column property="raceName" label="赛事名称" width="150"></el-table-column>
        <el-table-column property="raceStation" label="分站" width="200"></el-table-column>
        <el-table-column property="raceLevel" label="赛事级别"></el-table-column>
        <el-table-column property="racePost" :formatter="postFormatter" label="担任职位"></el-table-column>
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "Judge",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      name: "",
      form: {},
      dialogTableVisible:false,
      dialogFormVisible: false,
      dialogPostFormVisible:false,
      multipleSelection: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      post:"",
      postData:{},
      recordData:[],//执裁记录,
      raceDate:'',
      userIdcard:"",
      raceNames:[],
      raceId:this.$route.query.raceId
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
    const data = {
            "page":this.pageNum,
            "pageSize":this.pageSize,
            "name":this.name,
            "raceId":this.raceId
          }
      this.request.post("/judge/page", data).then(res => {
        //this.tableData = res.data.records
        this.tableData = res.data.data
        this.total = res.data.total
      })
    },
    loadRecord(){
      const data={
        "raceDate":this.raceDate,
        "userIdcard":this.userIdcard
        // "raceName":this.raceName
      }
      this.request.post("/judge/queryRecord",JSON.stringify(data)).then(res => {
        if (res.code === 200) {
          this.recordData = res.data
        } else {
          this.$message.error(res.message)
        }
      })
    },
    postFormatter(row){
      let post = "";
      //兼容执裁记录的字段racePost
      if(row.post ===undefined){
        post = row.racePost
      }else {
        post = row.post
      }
      switch (post) {
        case "0": return "技术代表"
          break;
        case "1": return "仲裁"
          break;
        case "2": return "裁判长"
          break;
        case "3": return "副裁判长"
          break;
        case "4": return "裁判员"
          break;
      }
    },
    acceptFormatter(row){
      if(row.isAccept ==="0"){
        return "同意"
      }else if(row.isAccept==="1"){
        return "拒绝"
      }
    },
    sexFormatter(row){
      if(row.sex ==="0"){
        return "男"
      }else {
        return "女"
      }
    },
    levelFormatter(row){
      switch (row.judgeLevel) {
        case "0": return "国际级"
          break;
        case "1": return "国家级"
          break;
        case "2": return "预备国家级"
          break;
        // case "3": return "二级"
        //   break;
        // case "4": return "三级"
        //   break;
      }
    },
    save() {
      this.request.post("/judge/save", this.form).then(res => {
        if (res.code === 200) {
          this.$message.success(res.message)
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    /**
     * 查询执裁记录
     * @param row
     */
    queryRecord(row){
      this.dialogTableVisible = true
      const data={
        "raceDate":this.raceDate,
        "userIdcard":row.idcard
        // "raceName":this.raceName
      }
      this.request.post("/judge/queryRecord",JSON.stringify(data)).then(res => {
        if (res.code === 200) {
          this.recordData = res.data
          this.userIdcard = res.data[0].userIdcard
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
      this.$nextTick(() => {
        if(this.$refs.img) {
           this.$refs.img.clearFiles();
         }
         if(this.$refs.file) {
          this.$refs.file.clearFiles();
         }
      })
    },
    handleEdit(row) {
      // this.form = JSON.parse(JSON.stringify(row))
      this.$confirm('选派岗位默认接受报名, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // if(row.isAccept ==="1"){
        //   return  this.$message.error("被拒绝的无法选派岗位！")
        // }
        this.dialogPostFormVisible = true
        this.postData={
          "id":row.id,
          "userIdcard":row.idcard,
          "raceId":row.raceId,
          "raceName":row.raceName
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });


    },
    /**
     * 选派岗位
     */
    selectPost(){
      this.postData.racePost = this.post;
      this.postData.isAccept = "0";
      this.postData.flag = "post";
      this.request.post("/judge/save",JSON.stringify(this.postData)).then(res => {
        if (res.code === 200) {
          this.$message.success(res.message)
          this.dialogPostFormVisible = false
          this.load()
        } else {
          this.$message.error("操作失败")
        }
      })
    },
    refuse(row) {
      let isAccept = ""
      if(row.isAccept ==="0"){
        isAccept ="1";
      }else {
        isAccept ="0"
      }
      const data = {
        "id":row.id,
        "isAccept":isAccept,
        "post":"",//置空岗位
        "flag":"isAccept",
        "userIdcard":row.idcard
      }
      this.request.post("/judge/save",JSON.stringify(data)).then(res => {
        if (res.code === 200) {
          this.$message.success(res.message)
          this.load()
        } else {
          this.$message.error("操作失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      if (!this.multipleSelection.length) {
        this.$message.error("请选择需要删除的数据")
        return
      }
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/judge/del/batch", ids).then(res => {
        if (res.code === 200) {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.name = ""
      this.load()
    },
    recordReset() {
      this.raceDate = ""
      // this.raceName=""
      this.loadRecord()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    handleFileUploadSuccess(res) {
      this.form.file = res
    },
    handleImgUploadSuccess(res) {
      this.form.img = res
    },
    download(url) {
      window.open(url)
    },
    exp() {
      window.open("http://localhost:9090/judge/export")
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.load()
    }
  }
}
</script>


<style>
.headerBg {
  background: #eee!important;
}
  .el-button{
    margin: 0px 6px 12px;
  }
</style>
